package com.rykj.qxj.server.services.impl;

import com.alibaba.fastjson.JSONObject;
import com.rykj.qxj.server.services.IQuartzService;
import com.rykj.qxj.server.util.ExternalConfig;
import com.rykj.qxj.server.util.HttpClientUtils;
import com.rykj.qxj.server.util.object.StringUtils;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service("quartzService")
@Log4j
public class QuartzServiceImpl implements IQuartzService {

    @Resource
    private ExternalConfig config;


    @Override
    public void addJob(String taskName, Integer taskId, String executeRate) throws  Exception{

        String monitorUrlUrl= String.format(config.getMonitorUrl());
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("taskName",taskName);
        paraMap.put("taskId",taskId);
        paraMap.put("executeRate",executeRate);
        String rest = HttpClientUtils.doPost(monitorUrlUrl+"/v1/quartz/addJob",paraMap);
        if(StringUtils.isEmpty(rest) || !JSONObject.parseObject(rest).getBoolean("success")){
            log.error("===添加任务=="+taskName+"==失败===="+rest);
            throw  new Exception("添加任务失败");
        }
    }

    @Override
    public void modifyJobTime(String taskName, Integer taskId, String executeRate) throws  Exception{

        String monitorUrlUrl= String.format(config.getMonitorUrl());
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("taskName",taskName);
        paraMap.put("taskId",taskId);
        paraMap.put("executeRate",executeRate);
        String rest=  HttpClientUtils.doPost(monitorUrlUrl+"/v1/quartz/modifyJobTime",paraMap);
        if(StringUtils.isEmpty(rest) || !JSONObject.parseObject(rest).getBoolean("success")){
            log.error("===修改任务=="+taskName+"==失败===="+rest);
            throw  new Exception("修改任务");
        }
    }

    @Override
    public void removeJob(String taskName) throws  Exception{

        String monitorUrlUrl= String.format(config.getMonitorUrl());
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("taskName",taskName);
        String rest=   HttpClientUtils.doGet(monitorUrlUrl+"/v1/quartz/removeJob",paraMap);
        if(StringUtils.isEmpty(rest) || !JSONObject.parseObject(rest).getBoolean("success")){
            log.error("===移除任务=="+taskName+"==失败===="+rest);
            throw  new Exception("移除任务失败");
        }
    }

    @Override
    public void startOrShutdownJob(String taskName, String taskStatus,Integer taskId,String time)  throws  Exception{

        String monitorUrlUrl= String.format(config.getMonitorUrl());
        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("taskName",taskName);
        paraMap.put("taskStatus",taskStatus);
        paraMap.put("taskId",taskId);
        paraMap.put("time",time);
        String rest =HttpClientUtils.doGet(monitorUrlUrl+"/v1/quartz/startOrShutdownJob",paraMap);
        if(StringUtils.isEmpty(rest) || !JSONObject.parseObject(rest).getBoolean("success")){
            log.error("===启动或禁用任务=="+taskName+"==失败===="+rest);
            throw  new Exception("启动或禁用任务失败");
        }

    }
}
